#!/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin
USB=/mnt

umask 022
mount -t proc proc /proc
mount sysfs /sys -t sysfs
if [ -e /proc/cpu/alignment ]; then
   echo "3" > /proc/cpu/alignment
fi

echo 0 > /proc/sys/kernel/printk
echo 100 > /sys/class/backlight/pwm-backlight/brightness

mount_usb() {
found=0
for i in /dev/sda?;do
	if [ $i == "/dev/sda?" ];then
		break
	fi
	mount $i ${USB} 2>/dev/null

	if [ $? != 0 ];then 
		continue
	fi

	return 0
done

# Try to mount a USB without partition table
	mount /dev/sda  ${USB} 2>/dev/null
	return $?

}

rotation=0
if [ -e /etc/rotation ]; then
	read rotation < /etc/rotation
fi


# wait until the device node is created

echo "Checking for application software"
echo "---------------------------------"
echo "  "

cp /etc/fw_env.config /tmp/.

while [ 1 ];do
	echo "Waiting for USB Pen..."
	sleep 3

	mount_usb
	if [ $? == 0 ];then
		break
	fi
done

echo " "
echo "Starting Software Update"
echo "------------------------"
swupdate -i "${USB}/*.swu" -v
if [ $? == 0 ];then
	echo "SUCCESS !"
else
	echo "FAILURE !"
fi

while [ 1 ]; do
	echo "Please reboot the system !"
	sleep 90
	reboot
done

exit 0
